#ifndef  VSAPPLICATION_H
#define  VSAPPLICATION_H
#include "VSSystem.h"
#include "VSMain.h"
#include "VSMath.h"
#include "VSMemManager.h"
#include "VSCommand.h"
#include "VSRenderer.h"
#include "VSGraphicInclude.h"
namespace VSEngine2
{
//-------------------------------------------------------
#define DLCARE_APPLICATION(classname)\
	public:\
		static bool RegisterMainFactory();\
	private:\
		static bool InitialApplation();\
		static bool TerminalApplation();\
		static bool ms_bRegisterMainFactory;
//-------------------------------------------------------
#define IMPLEMENT_APPLICATION(classname)\
	static bool gs_bStreamRegistered_classname= classname::RegisterMainFactory ();\
 	bool classname::ms_bRegisterMainFactory = false;\
	bool classname::RegisterMainFactory()\
	{\
		if (!ms_bRegisterMainFactory)\
		{\
			VSMain::AddInitialFuntion(classname::InitialApplation);\
			VSMain::AddTerminalFuntion(classname::TerminalApplation);\
			ms_bRegisterMainFactory = true;\
		}\
		return ms_bRegisterMainFactory;\
	}\
	bool classname::InitialApplation()\
	{\
		classname::ms_pApplication = VS_NEW classname();\
		if(!classname::ms_pApplication)\
			return false;\
		return true;\
	}\
	bool classname::TerminalApplation()\
	{\
		if (classname::ms_pApplication)\
		{\
			VS_DELETE(classname::ms_pApplication);\
		}\
		return true;\
	}
//-------------------------------------------------------
	class /*VSAPPLICATION_API*/ VSApplication : public VSMemObject
	{
	public:
		VSApplication();
		virtual ~VSApplication();
		static VSApplication * ms_pApplication;

	protected:

		VSCommand * m_pCommand;
		virtual bool CreateDx9();
		virtual bool CreateDx10();
		virtual bool CreateDx11();
		virtual bool CreateOPGL();
		virtual bool CreateAppWindow();

		HWND	m_MainHwnd;
		VSArray<VSRenderer::ChildWindowInfo>	m_ArrayChildHwnd;
		HINSTANCE m_hInst;

		unsigned int m_uiRenderAPIType;
		unsigned int m_uiScreenWidth;
		unsigned int m_uiScreenHeight;
		bool		m_bIsWindowed;
		unsigned int m_uiAnisotropy;
		unsigned int m_uiMultisample;
		VSString m_Tile;
		VSString m_WindowClassName;
		unsigned int m_uiInputAPIType;

		bool m_bIsActive;
		bool m_bIsRunning;
		static DWORD ms_WinodwKeyToVS[];
		bool m_bInitLoading;
		
		DWORD m_dwWindowedStyle;
		DWORD m_dwFullscreenStyle;

		unsigned int m_iUpdateThreadNum;
		static LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
		static void InputMsgProc(unsigned int uiInputType,unsigned int uiEvent,unsigned int uiKey,int x, int y, int z);
		virtual bool CreateRenderer();
		virtual bool CreateInput();
		virtual bool CreateTimer();
		virtual bool CreateSceneManager();
		virtual bool CreateWorld();
		virtual bool ReleaseRenderer();
		virtual bool ReleaseInput();
		virtual bool ReleaseTimer();
		virtual bool ReleaseSceneManager();
		virtual bool ReleaseWorld();
		virtual bool Run();
		virtual bool Loading();
		virtual bool CreateMonitor();
		virtual bool CreateASYNLoader();
		virtual bool CreateRenderThread();
		virtual bool CreateUpdateThread();
		virtual bool ReleaseMonitor();
		virtual bool ReleaseASYNLoader();
		virtual bool ReleaseRenderThread();
		virtual bool ReleaseUpdateThread();
		unsigned int CheckVirtualKeyDown(unsigned int VK);
		unsigned int CheckVirtualKeyUp(unsigned int VK);
	public:
		bool Main(HINSTANCE hInst,LPSTR lpCmdLine, int nCmdShow);

		virtual bool PreInitial();
		virtual bool OnInitial();	
		virtual bool OnTerminal();
		virtual bool PostUpdate();
		virtual bool PreUpdate();
		virtual bool OnDraw();
		virtual bool OnLoading();
		virtual void OnMove(int xPos,int yPos);
		virtual void OnReSize(int iWidth,int iHeight);
		virtual void OnKeyDown(unsigned int uiKey);
		virtual void OnKeyUp(unsigned int uiKey);
		virtual void OnLButtonDown(int xPos,int yPos);
		virtual void OnLButtonUp(int xPos,int yPos);
		virtual void OnRButtonDown(int xPos,int yPos);
		virtual void OnRButtonUp(int xPos,int yPos);
		virtual void OnMButtonDown(int xPos,int yPos);
		virtual void OnMButtonUp(int xPos,int yPos);
		virtual void OnMouseMove(int xPos,int yPos);
		virtual void OnMouseWheel(int xPos,int yPos,int zDet);

		virtual void ChangeScreenSize(unsigned int uiWidth,unsigned int uiHeight,bool bWindow,bool IsMaxScreen = false);
	};
	//IMPLEMENT_APPLICATION(VSApplication);
}
#endif